/*******************************************************************************
 * Copyright (c) 2006, 2008 Tom Schindl and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl - initial API and implementation
 *******************************************************************************/

package com.gcsf.books.gui.dialogs;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Shows how to setup a Viewer to start cell editing on double click
 * 
 * @author Tom Schindl <tom.schindl@bestsolution.at>
 * 
 */
public class Snippet052DouleClickCellEditor {

  private class MyContentProvider implements IStructuredContentProvider {

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java
     * .lang.Object)
     */
    public Object[] getElements(Object inputElement) {
      return (MyModel[]) inputElement;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
     */
    public void dispose() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface
     * .viewers.Viewer, java.lang.Object, java.lang.Object)
     */
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

  }

  public static boolean flag = true;

  public class MyModel {
    public int counter;

    public MyModel(int counter) {
      this.counter = counter;
    }

    public String toString() {
      return "Item " + this.counter;
    }
  }

  public Snippet052DouleClickCellEditor(Shell shell) {
    final TableViewer v = new TableViewer(shell, SWT.BORDER
        | SWT.FULL_SELECTION);
    v.setContentProvider(new MyContentProvider());
    v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getTable()),
        new TextCellEditor(v.getTable()), new TextCellEditor(v.getTable()) });
    v.setCellModifier(new ICellModifier() {

      public boolean canModify(Object element, String property) {
        return true;
      }

      public Object getValue(Object element, String property) {
        return "Column " + property + " => " + element.toString();
      }

      public void modify(Object element, String property, Object value) {

      }

    });

    v.setColumnProperties(new String[] { "1", "2", "3" });

    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
        v) {
      protected boolean isEditorActivationEvent(
          ColumnViewerEditorActivationEvent event) {
        return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
            || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
            || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
      }
    };

    TableViewerEditor.create(v, actSupport,
        ColumnViewerEditor.TABBING_HORIZONTAL
            | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
            | ColumnViewerEditor.TABBING_VERTICAL
            | ColumnViewerEditor.KEYBOARD_ACTIVATION);

    TableViewerColumn column = new TableViewerColumn(v, SWT.NONE);
    column.getColumn().setWidth(200);
    column.getColumn().setMoveable(true);
    column.getColumn().setText("Column 1");
    column.setLabelProvider(new ColumnLabelProvider());

    column = new TableViewerColumn(v, SWT.NONE);
    column.getColumn().setWidth(200);
    column.getColumn().setMoveable(true);
    column.getColumn().setText("Column 2");
    column.setLabelProvider(new ColumnLabelProvider());

    column = new TableViewerColumn(v, SWT.NONE);
    column.getColumn().setWidth(200);
    column.getColumn().setMoveable(true);
    column.getColumn().setText("Column 3");
    column.setLabelProvider(new ColumnLabelProvider());

    MyModel[] model = createModel();
    v.setInput(model);
    v.getTable().setLinesVisible(true);
    v.getTable().setHeaderVisible(true);
  }

  private MyModel[] createModel() {
    MyModel[] elements = new MyModel[10];

    for (int i = 0; i < 10; i++) {
      elements[i] = new MyModel(i);
    }

    return elements;
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    Display display = new Display();

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    new Snippet052DouleClickCellEditor(shell);
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }

    display.dispose();

  }

}
